package com.example.mynews.UI;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.mynews.Adapter.Index_ViewpagerAdapter;
import com.example.mynews.Fragment.Fragment_Home;
import com.example.mynews.Fragment.Fragment_News;
import com.example.mynews.Fragment.Fragment_Political;
import com.example.mynews.Fragment.Fragment_Setting;
import com.example.mynews.Fragment.Fragment_Wisdom;
import com.example.mynews.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class Index extends AppCompatActivity {
    //组件
    private ImageView index_img;
    private ViewPager index_viewpager;
    private TabLayout index_tab;
    private ImageView Side_img;
    private TextView side_text;
    private DrawerLayout layout;

    private List<Fragment> list;

    //全局
    private String[] index_title={"主页","新闻","智慧","政要","设置"};
    private int[] index_unselect_chart={R.mipmap.home, R.mipmap.newscenter, R.mipmap.smartservice, R.mipmap.govaffair, R.mipmap.setting};
    private int[] index_select_chart={R.mipmap.home_press, R.mipmap.newscenter_press, R.mipmap.smartservice_press, R.mipmap.govaffair_press, R.mipmap.setting_press};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        initView();
        inifuction();
    }
    private void initView() {
        index_img = (ImageView) findViewById(R.id.index_img);
        index_viewpager = (ViewPager) findViewById(R.id.index_viewpager);
        index_tab = (TabLayout) findViewById(R.id.index_tab);
        Side_img = (ImageView) findViewById(R.id.Side_img);
        side_text = (TextView) findViewById(R.id.side_text);
        layout = (DrawerLayout) findViewById(R.id.layout);
    }
    private void inifuction() {
        //设置侧边栏
        index_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layout.openDrawer(Gravity.LEFT);
            }
        });
        //设置viewpager
        list=new ArrayList<>();
        list.add(new Fragment_Home());
        list.add(new Fragment_News());
        list.add(new Fragment_Wisdom());
        list.add(new Fragment_Political());
        list.add(new Fragment_Setting());
        Index_ViewpagerAdapter index_viewpagerAdapter=new Index_ViewpagerAdapter(getSupportFragmentManager(),list);
        index_viewpager.setAdapter(index_viewpagerAdapter);
        index_tab.setupWithViewPager(index_viewpager);
        //因为tablayout和viewpager绑定后，自动添加tab，所以直接gettabat(i)，直接设置
        for (int i = 0; i < index_title.length; i++) {
            index_tab.getTabAt(i).setText(index_title[i]).setIcon(index_unselect_chart[i]);
        }
        //取消tab下划线
      //  index_tab.setSelectedTabIndicatorHeight(0);
//        //选中第一个tab标签
        index_tab.getTabAt(0).setIcon(index_select_chart[0]);
        index_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                for (int i=0;i<index_title.length;i++){
                    if (tab.getText().equals(index_title[i])){
                        tab.setIcon(index_select_chart[i]);
                    }
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                for (int i=0;i<index_title.length;i++){
                    if (tab.getText().equals(index_title[i])){
                        tab.setIcon(index_unselect_chart[i]);
                    }
                }
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });


    }
}